package enumandlambda;

/**
 * @author shkstart
 * synchronized修饰普通方法
 * @create 2022-12-03-23:21
 */
public class ThreaSynchronized2 {
    private static int number = 0;

    static class Counter {


        private static int MAX_COUNT = 1000000;

        public  Counter(int MAX_COUNT) {
            this.MAX_COUNT = MAX_COUNT;
        }

        public synchronized  void incr() {
            for (int i = 0; i < MAX_COUNT; i++) {
                number++;
            }
        }
        public synchronized  void decr() {
            for (int i = 0; i < MAX_COUNT; i++) {
                number--;
            }
        }

    }

    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter(100000);
        Thread t1= new Thread(()->{
            counter.incr();
        });
        Thread t2 = new Thread(()->{
            counter.decr();
        });
        t1.start();
        t2.start();

        //等待线程执行完成
        t1.join();
        t2.join();
        System.out.println("最终的结果"+number);
    }
}
